package info.openlvb.openlvg.client.base.home;

import java.util.ArrayList;

import info.openlvb.openlvg.client.images.OpenPageImages;
import info.openlvb.openlvg.client.service.GetConfigVserverService;
import info.openlvb.openlvg.client.service.GetConfigVserverServiceAsync;
import info.openlvb.openlvg.client.service.GraphService;
import info.openlvb.openlvg.client.service.GraphServiceAsync;
import info.openlvb.openlvg.client.service.Vserver;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;
import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Tool;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.portal.Portal;
import com.gwtext.client.widgets.portal.PortalColumn;
import com.gwtext.client.widgets.portal.Portlet;

public class HomePanel extends VerticalPanel{
	
	//private final GetConfigVserverServiceAsync GetConfigVserver = GWT.create(GetConfigVserverService.class);
	OpenPageImages images = GWT.create(OpenPageImages.class);
	

	public HomePanel(){
		RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET,
        "vserverConfig.xml");
    try {
      requestBuilder.sendRequest(null, new RequestCallback() {
        public void onError(Request request, Throwable exception) {
          requestFailed(exception);
        }
       public void onResponseReceived(Request request, Response response) {
          renderXML(response.getText());
        }
      });
    } catch (RequestException ex) {
      requestFailed(ex);
    }
		
        
        
		setBorderWidth(2);
		setWidth("100%");
		Portal portal = new Portal();
		//portal.setWidth("98%");
		portal.setMargins(15);
		portal.getAutoWidth();
		portal.getAutoHeight();
		portal.setAutoScroll(true);

		//create some shared tools  
		Tool gear = new Tool(Tool.GEAR, new Function() {  
			public void execute() {  
				MessageBox.alert("Message", "The Settings Tool was clicked");  
			}  
		});  

		Tool close = new Tool(Tool.CLOSE, new Function() {  
			public void execute() {  
				MessageBox.alert("Message", "The Settings Tool was clicked");  
			}  
		});  

		Tool[] tools = new Tool[]{gear, close};
		

		//create portal columns  
		/** Modification : Xavier
		 *  Modification de la largeur des colonnes du portal
		 */
		PortalColumn firstCol = new PortalColumn();
		firstCol.setWidth("31%");
		firstCol.setPaddings(10, 10, 0, 10);
		PortalColumn secondCol = new PortalColumn();
		secondCol.setWidth("31%");
		secondCol.setPaddings(10, 10, 0, 10);
		PortalColumn thirdCol = new PortalColumn();
		thirdCol.setWidth("31%");
		thirdCol.setPaddings(10, 10, 0, 10);

		//add portlets to portal columns  
		final Portlet Host = new Portlet();  
		Host.setTitle("Host");  
		Host.setLayout(new FitLayout());  
		Host.setTools(tools);
		Host.add(new RRDService(Host).getVp());
		Portlet Vserver1 = new Portlet();  
		Vserver1.setTitle("Vserver1");  
		Vserver1.setLayout(new FitLayout());  
		Vserver1.setTools(tools);  
		Vserver1.add(new RRDService(Vserver1).getVp());
		Portlet Vserver2 = new Portlet();  
		Vserver2.setTitle("Vserver2");  
		Vserver2.setLayout(new FitLayout());  
		Vserver2.setTools(tools);  
		Vserver2.add(new RRDService(Vserver2).getVp());

		firstCol.add(Host);
		secondCol.add(Vserver1);
		thirdCol.add(Vserver2);
		//add portal column to portal
		/** Modification : Xavier
		 *  Modification de la largeur des colonnes du portal
		 */
		//portal.add(firstCol, new ColumnLayoutData(.30));
		//portal.add(secondCol, new ColumnLayoutData(.30));
		portal.add(firstCol, new ColumnLayoutData(.31));
		portal.add(secondCol, new ColumnLayoutData(.31));
		portal.add(thirdCol, new ColumnLayoutData(.31));
		//CallVserverConfig();
		add(portal);
		
	}
	
//	private void CallVserverConfig (){
//		GetConfigVserver.GetConfigVserver(new AsyncCallback<ArrayList<Vserver>> () {
//
//			@Override
//			public void onFailure(Throwable caught) {
//			System.out.print("Test NOK du service VSERVER ");	
//			}
//			public void onSuccess(ArrayList<Vserver> result) {
//			System.out.print("Test du service VSERVER :"+result.get(0).toString());	
//			}
//		}
//		);
//	}
	private void requestFailed(Throwable exception) {
	    Window.alert("Failed to send the message: "
	        + exception.getMessage());
	  }
	private void renderXML(String xmlText) {
	    System.out.println(xmlText);
	    Document customerDom = XMLParser.parse(xmlText);
        Element customerElement = customerDom.getDocumentElement();
        // Must do this if you ever use a raw node list that you expect to be
        // all elements.
        XMLParser.removeWhitespace(customerElement);
        String nameValue = getElementTextValue(customerElement, "vserverName");
        System.out.println(nameValue);
	  }
	private String getElementTextValue(Element parent, String elementTag) {
        // If the xml is not coming from a known good source, this method would
        // have to include safety checks.
        return parent.getElementsByTagName(elementTag).item(0).getFirstChild()
          .getNodeValue();
      }
}


